/*
编写 : Hexh
时间 : 2012-7-13 15:30:53
*/
using System;
using System.ComponentModel;
using System.Web.Mvc;
using FsCarInfo.Core.Entity;
using FsCarInfo.Facade;
using HMIS.FrameWork;
using HMIS.Infrastructure.Core.Authority;
using HMIS.Infrastructure.Facade.Authority;
using HMIS.MVC.CommonSupport;
using HMIS.MVC.ViewModel;
using Iesi.Collections.Generic;

namespace HMIS.MVC.Controllers
{
	/// <summary>
	///	 the mvc class of FsArea
	/// </summary>
	public class FsAreaController : BaseController
	{

		private readonly IFsAreaFacade FsAreaFacade;
		private readonly IHospUserFacade HospUserFacade;
		private readonly IDepartmentFacade DepartmentFacade;

		public FsAreaController(IFsAreaFacade FsAreaFacade, IHospUserFacade HospUserFacade, IDepartmentFacade DepartmentFacade)
		{
			this.FsAreaFacade = FsAreaFacade;
			this.HospUserFacade = HospUserFacade;
			this.DepartmentFacade = DepartmentFacade;
		}

		[Description("访问FsArea管理页面")]
		[ViewPage]
		public ActionResult FsArea()
		{
			return View();
		}

		[ExtResult]
		[Description("添加修改FsArea信息")]
		public ActionResult SaveOrUpdate()
		{
			FsArea entity;
			if (String.IsNullOrEmpty(Request["Id"]))
			{
				entity = new FsArea
				{
					Name = Request["Name"],
					Code = Request["Code"],
					IsAppr = Request["IsAppr"] == null ? false : Request["IsAppr"] == "1",
					Description = Request["Description"],
					Departments = new HashedSet<Department>(),
					AuthorUsers = new HashedSet<HospUser>(),
				};
			}
			else
			{
				// get the entity.
				entity = FsAreaFacade.Get(Request["Id"]);
				// set new value..
				entity.Name = Request["Name"];
				entity.Code = Request["Code"];
				entity.Description = Request["Description"];
				entity.IsAppr = Request["IsAppr"] == null ? false : Request["IsAppr"] == "1";
				entity.Departments.Clear();
				entity.AuthorUsers.Clear();
			}
			// 管理机构
			string[] depts = Request["TmpDepartments"].Split(',');
			for (int i = 0; i < depts.Length; i++)
			{
				if (!string.IsNullOrEmpty(depts[i]))
				{
					entity.Departments.Add(DepartmentFacade.Load(depts[i]));
				}
			}
			// 授权用户
			string[] users = Request["TmpUsers"].Split(',');
			for (int i = 0; i < users.Length; i++)
			{
				if (!string.IsNullOrEmpty(users[i]))
				{
					entity.AuthorUsers.Add(HospUserFacade.Load(users[i]));
				}
			}
			// 保存信息
			FsAreaFacade.SaveOrUpdate(entity);
			return this.JsonFormat(new ExtResult { success = true, id = entity.Id });
		}

		[ExtResult]
		[Description("删除FsArea基本资料")]
		public ActionResult Delete()
		{
			FsAreaFacade.Delete(Request["Id"]);
			return this.JsonFormat((new ExtResult { success = true }));
		}

		[ExtResult]
		[Description("物理删除FsArea基本资料")]
		public ActionResult PhysicsDelete()
		{
			FsAreaFacade.PhysicsDelete(Request["Id"]);
			return this.JsonFormat((new ExtResult { success = true }));
		}

		[Description("获取FsArea清单")]
		public ActionResult GetPlist()
		{
			long total;
			var data = FsAreaFacade.GetPlist(
				Request["q_query"],
				int.Parse(Request["start"] == null ? "0" : Request["start"]),
				int.Parse(Request["limit"] == null ? "0" : Request["limit"]),
				Request["sort"],
				Request["dir"],
				this.DesDataFilter(Request["filter"]),
				out total);
			//TODO : 修改关联查询返回的类型
			return this.JsonFormat(
				new ExtGirdData<FsArea> { total = total, data = data },
				new string[] { "" });
		}

		[Description("获取FsArea信息")]
		public ActionResult GetById()
		{
			var entity = FsAreaFacade.Get(Request["Id"]);
			return this.JsonFormat(
			   new ExtFormLoadData<FsArea> { data = new FsArea[] { entity } }, new string[] { "Departments", "AuthorUsers" }
			   );
		}

		[ExtResult]
		[Description("判断FsArea字段是否唯一")]
		public ActionResult IsFieldExist()
		{
			var result = FsAreaFacade.IsFieldExist(Request["FieldName"], Request[Request["FieldName"]], Request["Id"]);
			return this.JsonFormat(new ExtResult { success = !result, msg = "已存在相同的项!" });
		}
	}
}
